package com.heart.sell.controller.common;

import com.heart.sell.config.WechatAccountConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Demo例子
 *
 * @author Luo Zhen
 * @create 2019-04-26 9:31
 */
@RestController
@RequestMapping("/weixin")
@Slf4j
public class WeixinController {

    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code) {
        // 需要在后台配置回调域名
        // https://open.weixin.qq.com/connect/oauth2/authorize?
        // appid=wx37e5ddff7dc5282e&
        // redirect_uri=http://high.natapp1.cc/sell/weixin/auth&  ------ 回调域名需对应后台填入的回调域名相同且认证过 微信后台回给此地址发送code 参数 来换取access_token 凭据
        // response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
        log.info("进入auth方法...");
        log.info("appId={},secret={}", wechatAccountConfig.getAppId(), wechatAccountConfig.getSecret());

        // 通过此地址来拉取access_token
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wechatAccountConfig.getAppId() + "&secret=" + wechatAccountConfig.getSecret() + "&code=" + code + "&grant_type=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);
        log.info("response={}", response);


    }
}
